import Cookies from 'js-cookie'
import Vue from 'vue'
import Vuex from 'vuex'
import { tokenKey } from './constant'

Vue.use(Vuex);
let UserName = 'UserName';
let Select = 'menuIndex';

const pubJs = {
  methods: {
    // 获取本地TOKEN
    GetToken() {
      return Cookies.get(tokenKey)
    },
    // 本地储存TOKEN
    SetToken(token) {
      return Cookies.set(tokenKey, token)
    },
    // 删除本地TOKEN
    RemoveToken() {
      this.RemoveUserName()
      return Cookies.remove(tokenKey)
    },
    // 千分位
    thousand(val) {
      let reg = /(\d{1,3})(?=(\d{3})+(?:$|\.))/g;
      let result = (val + '').replace(reg, '$&,');
      return result
    },
    // 去除空格
    NoSpacesAllowed(val) {
      let reg = /\s+/g;
      let result = val.replace(reg, '');
      return result
    },
    //千分位格式化
    NumberFormat(value) {
      if (!value) {
          return '0'
      }
      const intPartFormat = value.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,') 
      return intPartFormat
    },
    //判断对象为空
    objIsEmpty(val) {
      // null or undefined
      if (val == null) return true;
    
      if (typeof val === 'boolean') return false;
    
      if (typeof val === 'number') return !val;
    
      if (val instanceof Error) return val.message === '';
    
      switch (Object.prototype.toString.call(val)) {
        // String or Array
        case '[object String]':
        case '[object Array]':
          return !val.length;
    
        // Map or Set or File
        case '[object File]':
        case '[object Map]':
        case '[object Set]': {
          return !val.size;
        }
        // Plain Object
        case '[object Object]': {
          return !Object.keys(val).length;
        }
      }
    
      return false;
    },
    //数组去重
    arrDeDuplication(arr) {
      return Array.from(new Set(arr))
    },
    //数组按照某一个属性重新组成二维数组
    arr1to2(arr,key) {
      let keyArr = arr.map(v=>v[key]);
      keyArr = this.arrDeDuplication(keyArr);

      let newArr = [];
      keyArr.forEach(e=>{
        let tempArr = arr.filter(v=>v[key] === e);
        newArr.push({
          name: tempArr[0][key],
          content: tempArr,
          length: tempArr.length
        });
      })

      return newArr
    },
    //数组排序
    arrSort(arr,key) {
      //取维度的分数，为空则显示 --
      if(this.activeDimension.dimensionString !== '总共分数') {
        arr.forEach(e=>{
          let ketVal = e.dimensionScore[this.activeDimension.dimension];
          ( ketVal || ketVal == 0 ) ? e[key] = ketVal : e[key] = '--'
        })
      }else{
        arr.forEach(e=>{
          ( e[key] || e[key] == 0 ) ? e[key] : e[key] = '--'
        })
      }
      
      //把分数为--的数据抽离出来
      let emptyScore = arr.filter(e=>e[key] === '--');
      let valiableScore = arr.filter(e=>e[key] !== '--');


      //排序
      valiableScore = valiableScore.sort(function(a,b){
        return b[key] - a[key]
      })

      //重新排序
      return [...valiableScore,...emptyScore];
    }
  }
};

export default pubJs
